package com.atguigu.srb.core.controller.api;


import com.atguigu.common.result.R;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author Rayyy
 * @since 2022-05-15
 */
@Api(tags = "借款人")
@RestController
@RequestMapping("/api/core/borrower")
@Slf4j
public class BorrowerController {

    @Resource
    BorrowerService borrowerService;

    /**
     * 保存借款人信息
     *
     * @param borrowerVO
     * @param request
     * @return
     */
    @ApiOperation("保存借款人信息")
    @PostMapping("/auth/save") //已登录的情况下才能保存
    public R save(@RequestBody BorrowerVO borrowerVO, HttpServletRequest request) {
        //从当前登登录的信息当中获取userId，不能传递别人的userId
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        borrowerService.saveBorrowerVOByUserId(borrowerVO, userId);
        return R.ok().message("信息提交成功！");
    }

    /**
     * 获取借款人认证状态
     *
     * @return
     */
    @ApiOperation("获取借款人认证状态")
    @GetMapping("/auth/getBorrowerStatus")
    public R getBorrowerStatus(HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        Integer status = borrowerService.getStatusByUserId(userId);

        return R.ok().data("borrowerStatus",status);
    }
}

